package com.oocl.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.oocl.entity.Clients;
import com.oocl.service.ClientsService;

@Controller
public class ClientController {
	@Resource(name="clientsService")
	private ClientsService clientsService;
	
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String cname,String cpwd,HttpSession sen,Model model){
		Clients client = new Clients();
		client.setCname(cname);
		client.setCpwd(cpwd);
		client= clientsService.loadClient(client);
		if(client==null){
			model.addAttribute("info", "用户名或密码错误！");
			return "login";
		}else{	
			sen.setAttribute("cname", cname);
			sen.setAttribute("cid", client.getCid());
			
			return "redirect:showMerchants";
		}
	}
	
	
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String register(String cname,String cpwd,Model model){
		Clients client=new Clients();
		client.setCname(cname);
		client.setCpwd(cpwd);
		Clients cfromdb=new Clients();
		cfromdb=clientsService.findClientsByCname(cname);
		if(cfromdb!=null){
			model.addAttribute("info", "该用户名已存在！");
			return "register";
		}
		clientsService.addClient(client);
		model.addAttribute("info", "注册成功请重新登录！");
		return "login";
	}
	
}
